package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.mp.DataStatusEnum;
import com.linkhub.oms.dao.mapper.LogisticFreightTemplateEntityMapper;
import com.linkhub.oms.dao.mpservice.ILogisticFreightTemplateService;
import com.linkhub.oms.entity.LogisticFreightTemplateEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 物流运费模板表 服务实现类
 * </p>
 *
 * @author Frank
 * @since 2024-06-23
 */
@Service
public class LogisticFreightTemplateServiceImpl extends ServiceImpl<LogisticFreightTemplateEntityMapper, LogisticFreightTemplateEntity> implements ILogisticFreightTemplateService {

    @Override
    public List<LogisticFreightTemplateEntity> listByTemplateNames(List<String> templateNames) {
        if (CollUtil.isEmpty(templateNames)) {
            return Lists.newArrayList();
        }
        return super.lambdaQuery()
                .select(LogisticFreightTemplateEntity::getId,
                        LogisticFreightTemplateEntity::getTemplateName)
                .in(LogisticFreightTemplateEntity::getTemplateName, templateNames)
                .eq(LogisticFreightTemplateEntity::getDataStatus, DataStatusEnum.ENABLE)
                .list();
    }

    @Override
    public List<LogisticFreightTemplateEntity> listByChanelAndCountry(String channelCode, String countryCode) {
        if (StringUtils.isBlank(channelCode)) {
            return Lists.newArrayList();
        }
        return super.lambdaQuery()
                .select(LogisticFreightTemplateEntity::getId,
                        LogisticFreightTemplateEntity::getTemplateName,
                        LogisticFreightTemplateEntity::getCountryCode,
                        LogisticFreightTemplateEntity::getChannelCode,
                        LogisticFreightTemplateEntity::getCountryPartition,
                        LogisticFreightTemplateEntity::getEffectiveDate,
                        LogisticFreightTemplateEntity::getExpirationDate)
                .eq(LogisticFreightTemplateEntity::getChannelCode, channelCode)
                .eq(StringUtils.isNotBlank(countryCode), LogisticFreightTemplateEntity::getCountryCode, countryCode)
                .eq(LogisticFreightTemplateEntity::getDataStatus, DataStatusEnum.ENABLE)
                .list();
    }

    @Override
    public List<LogisticFreightTemplateEntity> listByChanelAndCountryAndDate(String channelCode,
                                                                             String countryCode,
                                                                             LocalDateTime validDate) {
        if (StringUtils.isBlank(channelCode) || StringUtils.isBlank(countryCode) || Objects.isNull(validDate)) {
            return Lists.newArrayList();
        }
        return super.lambdaQuery()
                .eq(LogisticFreightTemplateEntity::getChannelCode, channelCode)
                .eq(LogisticFreightTemplateEntity::getCountryCode, countryCode)
                .le(LogisticFreightTemplateEntity::getEffectiveDate, validDate)
                .ge(LogisticFreightTemplateEntity::getExpirationDate, validDate)
                .eq(LogisticFreightTemplateEntity::getDataStatus, DataStatusEnum.ENABLE)
                .orderByAsc(LogisticFreightTemplateEntity::getId)
                .list();
    }

    @Override
    public List<LogisticFreightTemplateEntity> listByChanelCodes(List<String> channelCodes) {
        if (CollUtil.isEmpty(channelCodes)) {
            return Lists.newArrayList();
        }

        return super.lambdaQuery()
                .select(LogisticFreightTemplateEntity::getId,
                        LogisticFreightTemplateEntity::getChannelCode,
                        LogisticFreightTemplateEntity::getCountryCode,
                        LogisticFreightTemplateEntity::getCountryPartition,
                        LogisticFreightTemplateEntity::getEffectiveDate,
                        LogisticFreightTemplateEntity::getExpirationDate)
                .in(LogisticFreightTemplateEntity::getChannelCode, channelCodes)
                .eq(LogisticFreightTemplateEntity::getDataStatus, DataStatusEnum.ENABLE)
                .orderByAsc(LogisticFreightTemplateEntity::getId)
                .list();
    }
}
